﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FTchina;
using System.Data;
using System.Collections;
using FTchina.Library;

public partial class UI_CallIn : System.Web.UI.Page
{
    string phone = string.Empty;
    Random r = new Random();
    string[] colors = { "#FFFFF4", "#F5FFE8", "#FFE6D9", "#F0FFF0", "#ECF5FF", "#FAF4FF" };//背景色
    public bool Sex = true;//前台判断是男是女


    protected void Page_Load(object sender, EventArgs e)
    {
        Response.End();
        //if (basic.CheckFreshTime())
        //{//如果在登陆限定时间内，就伪造一个id，欺骗登录检查系统


        //    MasterHelper.MasterID = MD5MK.MD5String(FTchina.basic.GetGuid());
        //}
        //else
        //{//如果已经超时，就让重新登录
        //    Response.Redirect("/ui/login.aspx?title=登录超时！请重新登陆&returnpage=" + Request.RawUrl.ToString());
        //}

        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Expires = 0;
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
        Response.AddHeader("pragma", "no-cache");
        Response.CacheControl = "no-cache";


        phone = FTchina.basic.SafeRequest(Request["phone"]);


        if (!IsPostBack)
        {

            BindSms_Example();
            ReceiveCall(phone);
        }
    }

    protected void BindSms_Example()
    {
        string tmp = string.Empty;
        string format = "  <input type='radio' name='smsactive' id='{0}'  /><span>{1}</span><br>";
        string sql = "select * from SMS_Example order by isdefault desc";

        using (DataTable dt = shujuku.GetDataTable(sql))
        {
            foreach (DataRow dr in dt.Rows)
            {
                tmp += string.Format(format, "rd" + dr["id"], dr["smscontent"]);
            }
        }

        litsms.Text = tmp;

    }



    /// <summary>
    /// 接收来电信息
    /// 新客户生成信息，老客户更新信息
    /// </summary>
    /// <param name="phone"></param>
    private void ReceiveCall(string phone)
    {
        GetUseadble();//获取可以预定的餐厅、房间
        litTargets.Text = phone;//显示来电号码

        string telphone = string.Empty;
        string landline = string.Empty;
        phone = FTchina.SMS.CustomerInfo.getFormatedPhone(phone, out telphone, out landline);
        txbCustomerTel0.Text = telphone;
        txbCustomerlandline0.Text = landline;

        litCustormInfo.Text = "新客户";
        if (phone.Length > 0 && FTchina.SMS.CustomerInfo.CheckCustomer(phone))
        {
            GetCustomerInfo(phone);//显示老客户的信息
        }



    }



    private void GetUseadble()
    {
        string sql = "select * from CustomerGroup order by id asc";
        ddlcg.DataSource = FTchina.shujuku.GetDataTable(sql);
        ddlcg.DataTextField = "groupname";
        ddlcg.DataValueField = "id";
        ddlcg.DataBind();
    }






    /// <summary>
    /// 获取用户信息，显示出来
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    private void GetCustomerInfo(string phone)
    {

        string CustomerInfo = "来电次数：{0}， 最后来电时间：{1} ";
        string id = FTchina.SMS.CustomerInfo.GetCustomerIDByPhone(phone).Trim();

        if (id.Length > 0)
        {
            FTchina.SMS.CustomerInfo.CallCountPlusOneBy(phone);//增加一次来电次数
            FTchina.Models.CustomerList mCustomerList = FTchina.BLL.CustomerListManager.GetCustomerListByid(Convert.ToInt32(id));
            litCustormInfo.Text = string.Format(CustomerInfo,
                mCustomerList.Callcount.ToString(),
                mCustomerList.LastCallDate.ToString("yyyy-MM-dd").Replace("1910-01-01", "")
                );
            txbcustomerName.Text = mCustomerList.CustomerName;
            txbCustomerTel0.Text = mCustomerList.CustomerTel0;
            txbCustomerlandline0.Text = mCustomerList.Customerlandline0;
            txbmemo.Text = mCustomerList.Memo;
            if (mCustomerList.CustormeSex == "女") Sex = false;//前台显示男女用
            //  ddlsex.SelectedValue = mCustomerList.CustormeSex;

            txbcustomerWork.Text = mCustomerList.CustomerWork;
            txbcustomerMeetAddress.Text = mCustomerList.CustomerMeetAddress;
            txbcustomerAddress0.Text = mCustomerList.CustomerAddress0;
            ddlcg.SelectedValue = mCustomerList.CustomerGroupID.ToString();
        }
    }











    /// <summary>
    /// 保存客户资料
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveCustomer_Click(object sender, EventArgs e)
    {
        string CustomerName = FTchina.basic.SafeRequest(txbcustomerName.Text);
        string CustormeSex = FTchina.basic.SafeRequest(Request["ddlsex"]);
        string CustomerGroupID = ddlcg.SelectedValue;
        string CustomerTel0 = FTchina.basic.SafeRequest(txbCustomerTel0.Text);
        string Customerlandline0 = FTchina.basic.SafeRequest(txbCustomerlandline0.Text);
        string CustomerWork = FTchina.basic.SafeRequest(txbcustomerWork.Text);
        string CustomerAddress0 = FTchina.basic.SafeRequest(txbcustomerAddress0.Text);
        string CustomerMeetAddress = FTchina.basic.SafeRequest(txbcustomerMeetAddress.Text);
        string Memo = FTchina.basic.SafeRequest(this.txbmemo.Text);
        string Nowdate = basic.GetSqliteDate();
        if (CustomerTel0.Trim().Length > 0)
        {
            if (!basic.ValidateMobile(CustomerTel0))
            {
                JS.Alert("手机号码输入错误！");
                return;
            }
        }
        SaveCustomerInfo(CustomerName, CustormeSex, CustomerGroupID, CustomerWork, CustomerTel0, Customerlandline0, CustomerAddress0, CustomerMeetAddress, Memo, Nowdate);
        CloseThis();
    }

    protected void btnok_Click(object sender, EventArgs e)
    {
        if (ChkSendSms.Checked)
        {
            if (txbSMS.Text.Trim().Length == 0)
            {
                JS.Alert("请输入短信内容！");
                return;
            }
        }



        string CustomerName = FTchina.basic.SafeRequest(txbcustomerName.Text);
        string CustormeSex = FTchina.basic.SafeRequest(Request["ddlsex"]);
        string CustomerGroupID = ddlcg.SelectedValue;
        string CustomerTel0 = FTchina.basic.SafeRequest(txbCustomerTel0.Text);
        string Customerlandline0 = FTchina.basic.SafeRequest(txbCustomerlandline0.Text);
        string CustomerWork = FTchina.basic.SafeRequest(txbcustomerWork.Text);
        string CustomerAddress0 = FTchina.basic.SafeRequest(txbcustomerAddress0.Text);
        string CustomerMeetAddress = FTchina.basic.SafeRequest(txbcustomerMeetAddress.Text);

        string Memo = FTchina.basic.SafeRequest(this.txbmemo.Text);
        string Nowdate = basic.GetSqliteDate();
        if (!basic.ValidateMobile(CustomerTel0))
        {
            JS.Alert("手机号码输入错误！");
            return;
        }


        /**
         * 1、如果客户是新客户，那么创建客户，如果是老客户，增加来电次数、更新来电时间
         * 2、插入一条预定记录，同时更新相关房间、餐桌的预定信息
         * 3、发送短信
         * */



        SaveCustomerInfo(CustomerName, CustormeSex, CustomerGroupID, CustomerWork, CustomerTel0, Customerlandline0, CustomerAddress0, CustomerMeetAddress, Memo, Nowdate);




        #region 发送短信

        if (ChkSendSms.Checked & txbSMS.Text.Trim().Length > 0)
        {
            string content = txbSMS.Text;
            string Result = FTchina.SMSServer.SMSSender.Send2SeverWithLog(phone, content);
            if (!basic.ValidateMobile(phone))
            {
                JS.Alert("手机号码输入错误，短信未发送，详情查看短信发送日志！");

            }
            else
            {
                if (Result.Contains("成功"))
                {

                    JS.Alert("短信已发送，详情查看短信发送日志！");
                }
                else
                {
                    FTchina.JS.Alert("发送失败，请查看发送历史记录" + Result);
                }
            }


        }
        #endregion


        CloseThis();
    }


    /// <summary>
    /// 保存客户信息
    /// </summary>
    /// <param name="CustomerName"></param>
    /// <param name="CustormeSex"></param>
    /// <param name="CustomerGroupID"></param>
    /// <param name="CustomerTel0"></param>
    /// <param name="Customerlandline0"></param>
    /// <param name="CustomerAddress0"></param>
    /// <param name="CustomerMeetAddress"></param>
    /// <param name="Memo"></param>
    /// <param name="Nowdate"></param>
    private void SaveCustomerInfo(string CustomerName, string CustormeSex, string CustomerGroupID, string CustomerWork, string CustomerTel0, string Customerlandline0, string CustomerAddress0, string CustomerMeetAddress, string Memo, string Nowdate)
    {
        if (Customerlandline0.Length > 0)
        {//如果有固话，就赋值
            phone = Customerlandline0;
        }
        if (CustomerTel0.Length > 0)
        {//如果有手机，后赋值
            phone = CustomerTel0;
        }

        if (FTchina.SMS.CustomerInfo.CheckCustomer(phone))
        {//老用户

            //老客户更新资料，
            string sql = "update CustomerList set CustomerName='{0}',CustormeSex='{1}',CustomerGroupID='{2}',CustomerAddress0='{3}',CustomerMeetAddress='{4}',MeetCount=MeetCount+1,LastCallDate='{5}',LastMeetDate='{6}',CustomerTel0='{7}',Customerlandline0='{8}',CustomerWork='{9}',Memo='{11}' where CustomerTel0='{10}' or Customerlandline0='{10}' ";
            string execsql = string.Format(sql, CustomerName, CustormeSex,
                CustomerGroupID, CustomerAddress0, CustomerMeetAddress,
                Nowdate, Nowdate, CustomerTel0, Customerlandline0, CustomerWork,
                phone, Memo);
            shujuku.ExecuteSql(execsql);
        }
        else
        {//新用户
            string insertsql = @"INSERT INTO [CustomerList]
           ([CustomerName]
           ,[CustormeSex]
           ,[CustomerTel0]
           ,[Customerlandline0]         
           ,[CustomerAddress0]
           ,[CustomerMeetAddress]
           ,[Memo]
           ,[CustomerGroupID]
           ,[Callcount]
           ,[MeetCount]
           ,[LastCallDate]
           ,[LastMeetDate],[CustomerWork])
     VALUES
           ('{0}'
           ,'{1}'
           ,'{2}'         
           ,'{3}'         
           ,'{4}'
           ,'{5}'
           ,'{6}'
           ,'1'
           ,'1'
           ,'{7}'
           ,'{8}'
           ,'{9}','{10}')";

            string execsql = string.Format(insertsql, CustomerName, CustormeSex, CustomerTel0, Customerlandline0, CustomerAddress0, CustomerMeetAddress,
                Memo, CustomerGroupID, Nowdate, Nowdate, CustomerWork);
            shujuku.ExecuteSql(execsql);
        }
    }






    protected void CloseThis()
    {

        JS.Alert("操作成功");
        if (FTchina.basic.SafeRequest(Request["close"]).Length > 0)
        {
            JS.CloseWindow();
            JS.CloseOpenerWindow();
        }
        else
        {
            JS.JavaScriptLocationHref("/ui/welcome.aspx");

        }

    }





}